<template>
  <el-dialog
    class="afterInvest-add-open custom-dialog"
    width="60%"
    :visible.sync="visible"
    @close="closeHandle"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <div slot="title" class="title">
      {{ title }}
    </div>
    <el-form
      ref="searchFrom1"
      :inline="true"
      label-width="80px"
      :model="searchFrom1"
      style="margin-top: 10px"
    >
      <el-form-item label="" prop="">
        <el-select v-model="allStrategyType" @change="selAllStrategyTypeHandle" placeholder="请选择" style="width: 120px;">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="allStrategyType==0" label="策略名称" prop="strategyName">
        <el-input
          v-model="searchFrom1.strategyName"
          clearable
          placeholder="请输入策略名称"
          style="width: 260px"
          @change="searchList()"
        ></el-input>
      </el-form-item>
      <el-form-item v-if="allStrategyType==1" label="组合名称" prop="compositionName">
        <el-input
          v-model="searchFrom1.compositionName"
          clearable
          placeholder="请输入组合名称"
          style="width: 260px"
          @change="searchList()"
        ></el-input>
      </el-form-item>
      <el-form-item v-if="allStrategyType==2" label="分类名称" prop="classfyName">
        <el-input
          v-model="searchFrom1.classfyName"
          clearable
          placeholder="请输入分类名称"
          style="width: 260px"
          @change="searchList()"
        ></el-input>
      </el-form-item>

      <el-form-item style="margin-left: 20px">
        <el-button type="primary" @click="searchList()">查询</el-button>
        <el-button @click="resetSearchFrom1()">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="filex">
      <el-table
        v-if="allStrategyType==0"
        ref="policyTable"
        :key="policyTable_key"
        v-loading="listLoading"
        :data="list"
        border
        fit
        size="mini"
        :row-class-name="tableRowClassName"
        @row-click="submitHandle"
        @row-dblclick="dblclickHandle"
        :header-cell-style="{ background: '#F5F7FA', color: '#333333' }"
        style="width: 100%"
        max-height="515"
      >
        <el-table-column
          v-if="type === 'id'"
          type="index"
          label="编号"
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span>{{ scope.$index }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-else
          type="index"
          label=""
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span v-if="type === 'id'">{{ scope.$index }}</span>
            <el-radio
              v-else-if="type === 'radio'"
              v-model="radio"
              :label="scope.row.strategyInfoVo.strategyId"
            >
              {{ "" }}
            </el-radio>
            <el-checkbox
              v-else
              v-model="checkArr"
              :label="'C'+scope.row.strategyInfoVo.strategyId"
              :disabled="!scope.row.strategyInfoVo.hasNetWorthStatus"
            >
              {{ "" }}
            </el-checkbox>
            <div class="checkhidden"></div>
          </template>
        </el-table-column>
        <el-table-column
          prop="strategyInfoVo.strategyName"
          label="策略名称"
          min-width="200"
          fixed
        >
        </el-table-column>
        <el-table-column
          prop="strategyInfoVo.investName"
          label="所属投顾"
          min-width="140"
         
        ></el-table-column>
        <el-table-column
          prop="strategyInfoVo.strategyMarket"
          label="投资市场"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="strategyInfoVo.strategyStyle"
          label="投资风格"
          min-width="80"
        ></el-table-column>
      </el-table>
      <el-table
        v-if="allStrategyType==1"
        ref="compositionTable"
        :key="compositionTable_key"
        v-loading="listLoading"
        :data="list"
        border fit
        size="mini"
        @row-click="submitHandle"
        @row-dblclick="dblclickHandle"
        :row-class-name="tableRowClassName"
        :header-cell-style="{ background: '#F5F7FA', color: '#333333' }"
        style="width: 100%;"
        max-height="515"
      >
        <el-table-column
          v-if="type === 'id'"
          type="index"
          label="编号"
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span>{{ scope.$index }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-else
          type="index"
          label=""
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span v-if="type === 'id'">{{ scope.$index }}</span>
            <el-radio
              v-else-if="type === 'radio'"
              v-model="radio"
              :label="scope.row.compositionId"
            >
              {{ "" }}
            </el-radio>
            <el-checkbox
              v-else
              v-model="checkArr"
              :label="'Z'+scope.row.compositionId"
              :disabled="!scope.row.hasNetWorth"
            >
              {{ "" }}
            </el-checkbox>
            <div class="checkhidden"></div>
          </template>
        </el-table-column>
        <el-table-column prop="compositionName" label="组合名称" min-width="120" fixed></el-table-column>
        <el-table-column prop="initMoney" label="投资金额(万元)" min-width="80" align="center">
          <template slot-scope="{row}">
            <el-tooltip effect="dark" :content="`已占用金额：${row.occupiedMoney||'/'}`" placement="top">
              <span>{{row.initMoney}}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="sinceCreatedIncomRate" label="收益率" min-width="100" align="center">
          <template slot-scope="{row}">
            <span v-if="row.sinceCreatedIncomRate" 
            :class="row.sinceCreatedIncomRate>=0?'positiveFc':'negativeFc'">{{ row.sinceCreatedIncomRate }}%</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="创建日期" min-width="100" align="center">
          <template slot-scope="{row}">
            <span>{{ row.createdTime | parseTime }}</span>
          </template>
        </el-table-column>
      </el-table>
      <el-table
        v-if="allStrategyType==2"
        ref="classifyTable"
        v-loading="listLoading"
        :data="list"
        border fit
        size="mini"
        :key="classifyTable_key"
        @row-click="submitHandle"
        @row-dblclick="dblclickHandle"
        :row-class-name="tableRowClassName"
        :header-cell-style="{ background: '#F5F7FA', color: '#333333' }"
        style="width: 100%;"
        max-height="515"
      >
        <el-table-column
          v-if="type === 'id'"
          type="index"
          label="编号"
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span>{{ scope.$index }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-else
          type="index"
          label=""
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span v-if="type === 'id'">{{ scope.$index }}</span>
            <el-radio
              v-else-if="type === 'radio'"
              v-model="radio"
              :label="scope.row.classifyId"
            >
              {{ "" }}
            </el-radio>
            <el-checkbox
              v-else
              v-model="checkArr"
              :label="'F'+scope.row.classifyId"
              :disabled="!scope.row.hasNetWorth"
            >
              {{ "" }}
            </el-checkbox>
            <div class="checkhidden"></div>
          </template>
        </el-table-column>
        <el-table-column prop="classfyName" label="分类名称" min-width="160" fixed></el-table-column>
        <el-table-column prop="compositionName" label="所属组合" min-width="160" fixed></el-table-column>
        <el-table-column prop="initMoney" label="投资金额(万元)" width="130" align="center"></el-table-column>
        <el-table-column prop="sinceCreatedIncomRate" label="收益率" min-width="100" align="center">
          <template slot-scope="{row}">
            <span v-if="row.sinceCreatedIncomRate" 
            :class="row.sinceCreatedIncomRate>=0?'positiveFc':'negativeFc'">{{ row.sinceCreatedIncomRate }}%</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="创建日期" min-width="100" align="center">
          <template slot-scope="{row}">
            <span>{{ row.createdTime | parseTime }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="list" v-if="type === 'check'">
        <div class="list-title">
          <span>已勾选</span>
          <span>({{ checkList.length }}/{{surplusNum}})</span>
        </div>
        <div style="height: 468px; overflow-y: scroll">
          <div
            class="list-item"
            v-for="(item, index) in checkList"
            :key="index"
          >
            <span class="list-item-content">
              {{ item.name }}
            </span>
            <i class="el-icon-close close" @click="delList(index)"></i>
          </div>
        </div>
      </div>
    </div>

    <div>
      <el-button
        v-if="type !== 'id'"
        style="float: right; margin-top: 15px; width: 160px; font-size: 12px"
        type="primary"
        size="small"
        @click="submitCheck()"
      >
        确认
      </el-button>
      <pagination
        style="text-algin: left"
        v-show="total > 0"
        :total="total"
        :layout="'total, sizes, prev, pager, next'"
        :page.sync="listQuery.pageCurrent"
        :limit.sync="listQuery.limit"
        @pagination="paginationHandle"
      />
    </div>
  </el-dialog>
</template>

<script>
import { fetchList } from "@/api/policyManage";
import { findStrategyCompositionByPage } from "@/api/combManage";
import { findStrategyClassifyByPage } from "@/api/groupManage";
import Pagination from "@/components/Pagination";

export default {
  props: {
    userId: String | Number,
    title: { type: String, default: "更改策略（单选）" },
    type: { type: String, default: "id" },
    surplusNum: { type: Number, default: 200 },
    nolink: {
      type: Boolean,
      default: false,
    },
  },
  components: { Pagination },
  data() {
    return {
      policyTable_key: 0,
      compositionTable_key: 0,
      classifyTable_key: 0,
      allStrategyType: 0,
      options: [
      {
        value: 0,
        label: '策略'
      }, 
      {
        value: 1,
        label: '组合'
      }, {
        value: 2,
        label: '分类'
      }, 
      {
        value: 3,
        label: '投后',
        disabled: true
      }
      ],
      noInvestName: false,
      visible: false,
      listLoading: false,
      total: 0,
      listQuery: {
        pageCurrent: 1,
        limit: 20,
      },
      searchFrom1: {
        investName: "",
        strategyName: "",
        compositionName: "",
        classfyName: "",
        searchType: 1,
      },
      list: [],
      checkList: [],
      radioList: [],
      radio: "",
      checkArr: [],
    };
  },
  methods: {
    paginationHandle() {
      const type = this.allStrategyType
      if(type == 0) {
        this.getPolicyList()
      }else if(type == 1) {
        this.findStrategyCompositionByPage()
      }else if(type == 2) {
        this.findStrategyClassifyByPage()
      }
    },
    selAllStrategyTypeHandle(type) {
      this.listQuery.pageCurrent = 1
      if(type == 0) {
        this.getPolicyList()
      }else if(type == 1) {
        this.findStrategyCompositionByPage()
      }else if(type == 2) {
        this.findStrategyClassifyByPage()
      }
    },
    tableRowClassName({ row, rowIndex }) {
      return "list-row";
    },
    closeHandle() {
      this.visible = false;
    },
    // 初始化
    init() {
      this.radio = "";
      this.visible = true;
      this.checkArr = [];
      this.checkList = [];
      this.listQuery = {
        pageCurrent: 1,
        limit: 20,
      };
      this.$nextTick(() => {
        this.$refs["searchFrom1"].resetFields();
        this.$refs.policyTable.clearSelection();
        this.getPolicyList();
      });
    },
    searchList() {
      this.listQuery.pageCurrent = 1
      if(this.allStrategyType==0) {
        this.getPolicyList()
      }else if(this.allStrategyType==1) {
        this.findStrategyCompositionByPage()
      }else if(this.allStrategyType==2) {
        this.findStrategyClassifyByPage()
      }
    },
    findStrategyClassifyByPage() {
      return new Promise((resolve) => {
        this.listLoading = true;
        findStrategyClassifyByPage({
          userId: this.userId,
            ...this.searchFrom1,
            ...this.listQuery,
        }).then(res => {
          console.log('分类列表', res);
          this.listLoading = false;
          this.list = res.body.list.map((items, index) => {
            items.id = index;
            return items;
          });
          this.total = res.body.total || 0;
          resolve(true);

          this.classifyTable_key++
        })
      })
    },
    findStrategyCompositionByPage() {
      return new Promise((resolve) => {
        this.listLoading = true;
        findStrategyCompositionByPage({
          userId: this.userId,
            ...this.searchFrom1,
            ...this.listQuery,
        }).then(res => {
          console.log('组合列表', res);
          this.listLoading = false;
          this.list = res.body.list.map((items, index) => {
            items.id = index;
            return items;
          });
          this.total = res.body.total || 0;
          resolve(true);
          this.compositionTable_key++
        })
      })
    },
    // 策略列表
    getPolicyList() {
      return new Promise((resolve) => {
        this.listLoading = true;
        fetchList({
          userId: this.userId,
          ...this.searchFrom1,
          ...this.listQuery,
        }).then((res) => {
          this.listLoading = false;
          this.list = res.body.map((items, index) => {
            items.id = index;
            return items;
          });
          console.log("策略列表", this.list);
          this.total = res.count || 0;
          resolve(true);
          this.policyTable_key++
        });
      });
    },
    resetSearchFrom1() {
      this.$refs["searchFrom1"].resetFields();
      if(this.allStrategyType==0) {
        this.getPolicyList()
      }else if(this.allStrategyType==1) {
        this.findStrategyCompositionByPage()
      }else if(this.allStrategyType==2) {
        this.findStrategyClassifyByPage()
      }
    },
    // 单选双击选中
    dblclickHandle(row) {
      if (this.type !== "check") {
        this.$emit("selctInvestName", row.strategyInfoVo);
        this.visible = false;
      }
    },
    // 行选中
    submitHandle(row, col) {
      if(this.allStrategyType == 0) {
        if(!row.strategyInfoVo.hasNetWorthStatus) {
          this.$message({
            message: '该产品净值不存在或者少于2条',
            warning: 'warning'
          })
          return
        }
      }else {
        if(!row.hasNetWorth) {
          this.$message({
            message: '该产品净值不存在或者少于2条',
            warning: 'warning'
          })
          return
        }
      }
      if(this.surplusNum<=this.checkArr.length) {
        this.$message({
          message: '最多添加20条对比数据',
          warning: 'warning'
        })
        return
      }
      if (this.type === "id") {
        this.$emit("selctInvestName", row.strategyInfoVo);
        this.visible = false;
      } else if (this.type === "radio") {
        this.radio = row.strategyInfoVo.strategyId;
        this.radioList = row;
      } else if (this.type === "check") {
        if(this.allStrategyType == 0) {
          if (this.checkArr.indexOf('C'+row.strategyInfoVo.strategyId) === -1) {
            this.checkArr.push('C'+row.strategyInfoVo.strategyId);
            this.checkList.push({
              id: row.strategyInfoVo.strategyId,
              type: 1,
              name: row.strategyInfoVo.strategyName
            });
          } else {
            for (var i = 0; i < this.checkArr.length; i++) {
              if (this.checkArr[i] === 'C'+row.strategyInfoVo.strategyId) {
                this.checkArr.splice(i, 1);
                this.checkList.splice(i, 1);
              }
            }
          }
        }else if(this.allStrategyType == 1) {
          if (this.checkArr.indexOf('Z'+row.compositionId) === -1) {
            this.checkArr.push('Z'+row.compositionId);
            this.checkList.push({
              id: row.compositionId,
              type: 3,
              name: row.compositionName
            });
          } else {
            for (let i = 0; i < this.checkArr.length; i++) {
              if (this.checkArr[i] === 'Z'+row.compositionId) {
                this.checkArr.splice(i, 1);
                this.checkList.splice(i, 1);
              }
            }
          }
        }else if(this.allStrategyType == 2) {
          if (this.checkArr.indexOf('F'+row.classifyId) === -1) {
            this.checkArr.push('F'+row.classifyId);
            this.checkList.push({
              id: row.classifyId,
              type: 2,
              name: row.classfyName
            });
          } else {
            for (let i = 0; i < this.checkArr.length; i++) {
              if (this.checkArr[i] === 'F'+row.compositionId) {
                this.checkArr.splice(i, 1);
                this.checkList.splice(i, 1);
              }
            }
          }
        }
      }
    },
    delList(i) {
      this.checkArr.splice(i, 1);
      this.checkList.splice(i, 1);
    },
    // 多选
    handleSelectionChange(rows) {
      this.checkList = rows.map((item) => {
        return item;
      });
    },
    // 确认
    submitCheck() {
      if (this.type === "radio" && this.radioList.id) {
        this.$emit("selctInvestName", this.radioList.strategyInfoVo);
        this.visible = false;
      } else if (this.type === "check" && this.checkList.length > 0) {
        this.$emit("selCheckctInvestName", this.checkList);
        this.visible = false;
      } else {
        this.$message({
          message: "请选择策略",
          type: "info",
          duration: 2 * 1000,
        });
      }
    },
  },
};
</script>

<style lang="scss">
.list-row {
  cursor: pointer;
}
</style>
<style lang="scss" scoped>
.afterInvest-add-open {
  ::v-deep .el-dialog__body {
    min-height: 600px;
    padding-top: 10px;
    padding-bottom: 10px;
  }
  ::v-deep .pagination-container {
    padding: 15px 0;
    text-align: left;
  }
  .title {
    line-height: 53px;
    background: #d4dfef;
    padding-left: 20px;
    color: #333333;
    font-weight: bold;
    font-size: 18px;
  }
  .title2 {
    margin-top: 15px;
  }
  .head {
    border: 1px solid #ddd;
    ::v-deep .el-form {
      margin-top: 15px;
    }
  }
  .filex {
    display: flex;
    justify-content: space-between;
    .list {
      margin-left: 15px;
      width: 280px;
      min-height: 200px;

      &-title {
        background: #f5f7fa;
        color: #333;
        font-weight: bold;
        height: 47px;
        padding-left: 16px;
        line-height: 36px;
      }
      &-item {
        background: #f5f7fa;
        margin-top: 10px;
        font-size: 14px;
        padding-left: 10px;
        line-height: 30px;
        &:hover {
          color: #1890ff;
        }
        &-content {
          width: 210px;
          display: inline-block;
        }
      }
      .close {
        padding: 0 10px;
        line-height: 30px;
        cursor: pointer;
      }
    }
  }
  .table-content {
    border: 1px solid #ddd;
    padding: 5px;
    font-size: 12px;
    font-weight: bold;
    // margin-top: 15px;
  }
  .checkhidden {
    display: inline-block;
    width: 80px;
    height: 34px;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 999;
  }
  ::v-deep .el-table__fixed {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
  ::v-deep .el-table__fixed-right {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
}
</style>

